<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
    <el-form-item label="课程编号" prop="courseCode">
      <el-input v-model="dataForm.courseCode" placeholder="课程编号"></el-input>
    </el-form-item>
    <el-form-item label="课程名称" prop="courseName">
      <el-input v-model="dataForm.courseName" placeholder="课程名称"></el-input>
    </el-form-item>
      <el-form-item label="上课时间" prop="courseStartTime">
      <!-- <el-input v-model="dataForm.lastLoginTime" placeholder="操作时间"></el-input> -->
      <div class="block">
    <el-date-picker
      v-model="dataForm.courseStartTime"
      type="datetime"
      placeholder="选择日期时间"
      value-format="yyyy-MM-dd HH:mm:ss">
    </el-date-picker>
  </div>
    </el-form-item>
  
    <el-form-item label="课程内容" prop="courseContent">
      <el-input type="textarea"
  :rows="4"
  placeholder="请输入内容" v-model="dataForm.courseContent" ></el-input>
    </el-form-item>

    
    <el-form-item label="上课人数" prop="coursePeopleAmount">
      <el-input v-model="dataForm.coursePeopleAmount" placeholder="上课人数"></el-input>
    </el-form-item>
    <el-form-item label="任课老师编号" prop="courseTeacherCode">
      <el-input v-model="dataForm.courseTeacherCode" placeholder="任课老师编号"></el-input>
    </el-form-item>
    <el-form-item label="任课老师名称" prop="courseTeacherName">
      <el-input v-model="dataForm.courseTeacherName" placeholder="任课老师名称"></el-input>
    </el-form-item>
    <el-form-item label="课程地址" prop="courseAddress">
      <el-input v-model="dataForm.courseAddress" placeholder="课程地址"></el-input>
    </el-form-item>
    <el-form-item label="课程图片" prop="courseDescription">
    <div class="addBrand-container" id="food-add-app">
    <div class="container">
        <el-upload class="avatar-uploader"
                action="http://localhost:88/api/course/common/upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeUpload"
                ref="upload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
    </div>
    </div>
  </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          gmtCreate: '',
          createBy: '',
          gmtModified: '',
          lastModifiedBy: '',
          isDeleted: '',
          sortNo: '',
          version: '',
          courseCode: '',
          courseName: '',
          courseAddress: '',
          courseStartTime: '',
          courseTime: '',
          courseContent: '',
          courseStatus: '',
          coursePeopleAmount: '',
          courseTeacherCode: '',
          courseTeacherName: '',
          courseDescription: ''
        },
        imageUrl:'',
        dataRule: {
          gmtCreate: [
            { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '记录创建者不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
          ],
          lastModifiedBy: [
            { required: true, message: '记录最后更新人不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除(1删除,0未删除)不能为空', trigger: 'blur' }
          ],
          sortNo: [
            { required: true, message: '序号不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本不能为空', trigger: 'blur' }
          ],
          courseCode: [
            { required: true, message: '课程编号不能为空', trigger: 'blur' }
          ],
          courseName: [
            { required: true, message: '课程名称不能为空', trigger: 'blur' }
          ],
          courseAddress: [
            { required: true, message: '课程地址不能为空', trigger: 'blur' }
          ],
          courseStartTime: [
            { required: true, message: '上课时间不能为空', trigger: 'blur' }
          ],
          courseTime: [
            { required: true, message: '课程总时长不能为空', trigger: 'blur' }
          ],
          courseContent: [
            { required: true, message: '课程内容不能为空', trigger: 'blur' }
          ],
          courseStatus: [
            { required: true, message: '课程状态（01_未开始,02_进行中,03_已结束）不能为空', trigger: 'blur' }
          ],
          coursePeopleAmount: [
            { required: true, message: '上课人数不能为空', trigger: 'blur' }
          ],
          courseTeacherCode: [
            { required: true, message: '任课老师编号不能为空', trigger: 'blur' }
          ],
          courseTeacherName: [
            { required: true, message: '任课老师名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/course/tcourseinformation/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gmtCreate = data.tCourseInformation.gmtCreate
                this.dataForm.createBy = data.tCourseInformation.createBy
                this.dataForm.gmtModified = data.tCourseInformation.gmtModified
                this.dataForm.lastModifiedBy = data.tCourseInformation.lastModifiedBy
                this.dataForm.isDeleted = data.tCourseInformation.isDeleted
                this.dataForm.sortNo = data.tCourseInformation.sortNo
                this.dataForm.version = data.tCourseInformation.version
                this.dataForm.courseCode = data.tCourseInformation.courseCode
                this.dataForm.courseName = data.tCourseInformation.courseName
                this.dataForm.courseAddress = data.tCourseInformation.courseAddress
                this.dataForm.courseStartTime = data.tCourseInformation.courseStartTime
                this.dataForm.courseTime = data.tCourseInformation.courseTime
                this.dataForm.courseContent = data.tCourseInformation.courseContent
                this.dataForm.courseStatus = data.tCourseInformation.courseStatus
                this.dataForm.coursePeopleAmount = data.tCourseInformation.coursePeopleAmount
                this.dataForm.courseTeacherCode = data.tCourseInformation.courseTeacherCode
                this.dataForm.courseTeacherName = data.tCourseInformation.courseTeacherName
                this.dataForm.courseDescription = data.tCourseInformation.courseDescription
              }
            })
          }
        })
      },          handleAvatarSuccess (response, file, fileList) {
               this.dataForm.courseDescription=response.data;
              this.imageUrl = `http://localhost:88/api/course/common/download?name=${response.data}`
          },
          beforeUpload (file) {
            if(file){
              const suffix = file.name.split('.')[1]
              const size = file.size / 1024 / 1024 < 2
              if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                this.$refs.upload.clearFiles()
                return false
              }
              if(!size){
                this.$message.error('上传文件大小不能超过 2MB!')
                return false
              }
              return file
            }
          },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/course/tcourseinformation/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'gmtCreate': this.dataForm.gmtCreate,
                'createBy': this.dataForm.createBy,
                'gmtModified': this.dataForm.gmtModified,
                'lastModifiedBy': this.dataForm.lastModifiedBy,
                'isDeleted': this.dataForm.isDeleted,
                'sortNo': this.dataForm.sortNo,
                'version': this.dataForm.version,
                'courseCode': this.dataForm.courseCode,
                'courseName': this.dataForm.courseName,
                'courseAddress': this.dataForm.courseAddress,
                'courseStartTime': this.dataForm.courseStartTime,
                'courseTime': this.dataForm.courseTime,
                'courseContent': this.dataForm.courseContent,
                'courseStatus': this.dataForm.courseStatus,
                'coursePeopleAmount': this.dataForm.coursePeopleAmount,
                'courseTeacherCode': this.dataForm.courseTeacherCode,
                'courseTeacherName': this.dataForm.courseTeacherName,
                'courseDescription':this.dataForm.courseDescription
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
